package com.whg.shiro;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * @Author whg
 * @create 2022/9/27 10:48
 * @description：
 */
public class QuickStartTest5_4 {

    private CustomRealm customRealm = new CustomRealm();

    private DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

    @BeforeEach
    public void init(){
        defaultSecurityManager.setRealm(customRealm);
        SecurityUtils.setSecurityManager(defaultSecurityManager);
    }

    @Test
    public void test(){

        Subject subject = SecurityUtils.getSubject();

        //用户输入的账号密码
        UsernamePasswordToken usernamePasswordToken =
                new UsernamePasswordToken("xdclass","1234");

        subject.login(usernamePasswordToken);

        System.out.println("认证结果："+subject.isAuthenticated());

        System.out.println("getPrincipal = " + subject.getPrincipal());

        System.out.println("是否有对应的admin角色：" + subject.hasRole("admin"));

        System.out.println("是否有video:add权限：" + subject.isPermitted("video:add"));

        subject.checkRole("admin");
    }

}
